package com.cs.sso.controller;

import com.cs.common.vo.ReturnVo;
import com.cs.sso.service.TokenService;
import com.cs.sso.vo.LoginToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("token")
public class TokenController {
    @Autowired
    private TokenService tokenService;

    @RequestMapping("check")
    public ReturnVo check(@RequestParam("token") String token) {
        ReturnVo returnVo = new ReturnVo();

        // 检查 redis 中的 token 是否有效
        LoginToken loginToken = tokenService.check(token);
        if ( null == loginToken ) {
            return returnVo.failure("token已失效");
        }
        Map<String, String> map = new HashMap<>();
        map.put("username", loginToken.getUsername());
        map.put("name", loginToken.getName());
        return returnVo.success("获取登录信息成功", map);
    }
}
